package xiaohong.data.linked;

import java.util.LinkedHashMap;
import java.util.Stack;

public class LinkedNodeDemo {


    public static void main(String[] args) {
        HeroEntity hero = new HeroEntity(1,"小宏","神");
        HeroEntity hero1 = new HeroEntity(2,"斯塔克","漫威钢铁侠");
        HeroEntity hero2 = new HeroEntity(4,"Jack","美国队长");
        HeroEntity hero3 = new HeroEntity(6,"孙悟空","齐天大圣");
        HeroEntity hero4 = new HeroEntity(5,"杨戬","二郎神");
        HeroEntity hero5 = new HeroEntity(3,"共产党","傻逼");

        new LinkedHashMap<>();
        SingleLinkedNode singleLinkedNode = new SingleLinkedNode();
//        singleLinkedNode.addHero(hero);
//        singleLinkedNode.addHero(hero1);
//        singleLinkedNode.addHero(hero2);
//        singleLinkedNode.addHero(hero3);
//        singleLinkedNode.addHero(hero4);
//        singleLinkedNode.addHero(hero5);
//
//        singleLinkedNode.list();

//        System.out.println("删除一个节点");
//        singleLinkedNode.delHero(2);
//        singleLinkedNode.list();

//        System.out.println("修改一个节点");
//        hero5.setName("最后");
//        hero5.setNickName("last");
//        singleLinkedNode.updateHero(hero5);
//        singleLinkedNode.list();
        singleLinkedNode.addOrderHero(hero4);
        singleLinkedNode.addOrderHero(hero1);
        singleLinkedNode.addOrderHero(hero2);
        singleLinkedNode.addOrderHero(hero3);
        singleLinkedNode.addOrderHero(hero5);
        singleLinkedNode.addOrderHero(hero);
        singleLinkedNode.list();

//        int stackLength = SingleLinkedNode.getStackLength(singleLinkedNode.getHead());
//        System.out.println("链表的有效数据为 = " + stackLength);

        int index = 1 ;
        HeroEntity indexHero = SingleLinkedNode.getIndexHero(singleLinkedNode.getHead(), index);
        System.out.println("倒数第" + index + "个节点为-" + indexHero);

        SingleLinkedNode.revertNode(singleLinkedNode.getHead());


    }





}


class SingleLinkedNode {

    HeroEntity head = new HeroEntity(0,"","");

    /**
     * 输出英雄
     */
    public void list(){
        if (head.getNext() == null){
            System.out.println("链表为空");
            return;
        }
        HeroEntity heroEntity = head.getNext();
        while (heroEntity != null) {
            // 输出英雄
            System.out.println(heroEntity);
            heroEntity = heroEntity.getNext();
        }
    }



    /**
     * 添加英雄
     * @param heroEntity 英雄
     */
    public void addHero(HeroEntity heroEntity){
        HeroEntity heroNext = head;
        while (heroNext.getNext() != null) {
            heroNext = heroNext.getNext();
        }
        heroNext.setNext(heroEntity);
    }


    /**
     * 添加英雄
     * @param heroEntity 英雄
     */
    public void addOrderHero(HeroEntity heroEntity){
        if (heroEntity == null || heroEntity.getNo() == 0){
            System.out.println("链表为空");
            return;
        }
        HeroEntity heroNext = head;
        boolean flag = false;
        while (heroNext.getNext() != null) {
            if (heroNext.getNext() == null){
                break;
            }
            if (heroNext.getNext().getNo() > heroEntity.getNo()){
                break;
            }else if (heroNext.getNext().getNo() == heroEntity.getNo()){
                flag = true;
                break;
            }
            heroNext = heroNext.getNext();
        }
        if (flag){
            System.out.println("编号已存在，不能添加英雄！");
        }else {
            heroEntity.setNext(heroNext.getNext());
            heroNext.setNext(heroEntity);
        }
    }


    /**
     * 删除英雄
     * @param no 英雄编号
     */
    public void delHero(int no){
        boolean flag = false;
        HeroEntity heroNext = head;
        while (heroNext.getNext()!= null) {
            if (heroNext.getNext() == null){
                break;
            }
            if (heroNext.getNext().getNo() == no) {
                flag = true;
                break;
            }
            heroNext = heroNext.getNext();
        }
        if (flag){
            System.out.println("删除的节点是：" + heroNext.getNext());
            heroNext.setNext(heroNext.getNext().getNext());
        }else {
            System.out.println("没有找到删除的节点");
        }
    }


    public void updateHero(HeroEntity heroEntity){
        boolean flag = false;
        HeroEntity heroNext = head;
        while (heroNext.getNext() != null) {
            if (heroNext.getNext() == null){
                break;
            }
            if (heroNext.getNext().getNo() == heroEntity.getNo()) {
                flag = true;
                break;
            }
            heroNext = heroNext.getNext();
        }
        if (flag){
            System.out.println("修改的节点是：" + heroNext.getNext());
            heroNext.setNext(heroEntity);
        }else {
            System.out.println("没有找到修改的节点");
        }

    }

    public HeroEntity getHead() {
        return head;
    }

    public void setHead(HeroEntity head) {
        this.head = head;
    }

    /**
     * 查询单链表中有效节点的个数
     * @return 单链表中有效节点的个数
     */
    public static int getStackLength(HeroEntity head) {
        HeroEntity heroEntity = head.getNext();
        int stackLength = 0;
        while (heroEntity != null) {
            stackLength++;
            heroEntity = heroEntity.getNext();
        }
        return stackLength;
    }

    public static HeroEntity getIndexHero(HeroEntity head, int index) {
        HeroEntity heroEntity = head.getNext();
        int stackLength = getStackLength(head);
        if (index > stackLength || index < 0) {
            return null;
        }
        //  stackLength = 6   index = 5  1
        for (int i = 0; i < stackLength - index; i++) {
            if (heroEntity == null){
                break;
            }
            heroEntity = heroEntity.getNext();
        }
        return heroEntity;
    }


    public static void revertNode(HeroEntity headNode) {
        HeroEntity heroEntity = headNode.getNext();
        Stack<HeroEntity> stack = new Stack<>();
        while (heroEntity != null) {
            stack.push(heroEntity);
            heroEntity = heroEntity.getNext();
        }
        while (!stack.isEmpty()) {
            heroEntity = stack.pop();
            System.out.println(heroEntity);
        }
    }




}


class HeroEntity{

    private int no ;
    private String name ;
    private String nickName ;
    private HeroEntity next;

    public HeroEntity() {}

    public HeroEntity(int no, String name, String nickName) {
        this.no = no;
        this.name = name;
        this.nickName = nickName;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickName='" + nickName + '\'' +
                '}';
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    public HeroEntity getNext() {
        return next;
    }

    public void setNext(HeroEntity next) {
        this.next = next;
    }


}
